#!/bin/sh

revision="usb devices v0.1.0 (2021-10-26 by MIBonk)"
# use --help for more info

export PATH=:/proc/boot:/sbin:/bin:/usr/bin:/usr/sbin:/net/mmx/bin:/net/mmx/usr/bin:/net/mmx/usr/sbin:/net/mmx/sbin:/net/mmx/mnt/app/armle/bin:/net/mmx/mnt/app/armle/sbin:/net/mmx/mnt/app/armle/usr/bin:/net/mmx/mnt/app/armle/usr/sbin
export LD_LIBRARY_PATH=/net/mmx/mnt/app/root/lib-target:/net/mmx/mnt/eso/lib:/net/mmx/eso/lib:/net/mmx/mnt/app/usr/lib:/net/mmx/mnt/app/armle/lib:/net/mmx/mnt/app/armle/lib/dll:/net/mmx/mnt/app/armle/usr/lib
export IPL_CONFIG_DIR=/etc/eso/production

thisname="$(basename $0)"
thisdir="$(dirname $0)"

if [ -z $LOG ]; then
	. $thisdir/../config/GLOBALS
	echo -ne "\n$ME-$thisname---->\n" >> $LOG
fi

if [ -f $TMP/reboot.mib ]; then
	echo "Reboot running..."
	return 2> /dev/null
fi

	case $1 in

	-a) {

[ -z "$GEM" ] && echo -ne "\033[36m"
echo -ne "Install more driver for USB devices...\n"
mount -uw /net/mmx/mnt/system/
rm -rf /net/mmx/mnt/system/etc/usblauncher_sda0.lua
cp -rf /net/mmx/fs/sda0/mod/usblauncher/usblauncher_sda0.lua /net/mmx/mnt/system/etc/usblauncher_sda0.lua
UDEVICE=$(cat /net/mmx/mnt/system/etc/usblauncher.lua | grep usblauncher_sda0.lua)
if [ -z "$UDEVICE" ] && [ ! -f /net/mmx/mnt/system/etc/usblauncher.bck ]; then
	cp -rf /net/mmx/mnt/system/etc/usblauncher.lua /net/mmx/mnt/system/etc/usblauncher.bck
	echo -ne "dofile '/etc/usblauncher_sda0.lua'\n\n" >> /net/mmx/mnt/system/etc/usblauncher.lua
fi
UDEVICE=$(cat /net/mmx/mnt/system/etc/usblauncher_otg.lua | grep usblauncher_sda0.lua)
if [ -z "$UDEVICE" ] && [ ! -f /net/mmx/mnt/system/etc/usblauncher_otg.bck ]; then
	cp -rf /net/mmx/mnt/system/etc/usblauncher_otg.lua /net/mmx/mnt/system/etc/usblauncher_otg.bck
	echo -ne "dofile '/etc/usblauncher_sda0.lua'\n\n" >> /net/mmx/mnt/system/etc/usblauncher_otg.lua
fi
echo -ne "more USB devices activated now\n\n" | $TEE -a $LOG
[ -z "$GEM" ] && echo -ne "\033[m"

. $thisdir/reboot -t 10

return 2> /dev/null

	};;

	-d) {

[ -z "$GEM" ] && echo -ne "\033[36m"
echo -ne "Deinstall more driver for USB devices...\n"
mount -uw /net/mmx/mnt/system/
rm -rf /net/mmx/mnt/system/etc/usblauncher_sda0.lua
cp -rf /net/mmx/fs/sda0/mod/usblauncher/usblauncher_sda0.lua /net/mmx/mnt/system/etc/usblauncher_sda0.lua
if [ -f /net/mmx/mnt/system/etc/usblauncher.bck ]; then
#	rm -rf /net/mmx/mnt/system/etc/usblauncher.lua
	mv -rf /net/mmx/mnt/system/etc/usblauncher.bck /net/mmx/mnt/system/etc/usblauncher.lua
fi
if [ -f /net/mmx/mnt/system/etc/usblauncher_otg.bck ]; then
#	rm -rf /net/mmx/mnt/system/etc/usblauncher_otg.lua
	mv -rf /net/mmx/mnt/system/etc/usblauncher_otg.bck /net/mmx/mnt/system/etc/usblauncher_otg.lua
fi
echo -ne "Standard USB devices activated now\n\n" | $TEE -a $LOG
[ -z "$GEM" ] && echo -ne "\033[m"

. $thisdir/reboot -t 10

return 2> /dev/null

	};;


	# help or unknown parameter ------------------------------
	*) {
		echo ""
		echo $revision
		echo ""
		echo "Usage: "$thisname" [OPTION]"
		echo ""
		echo "Options:"
		echo "        -a 			activated more USB devices"
		echo "        -d 			deactivated new USB decvices"
		echo "        --help		show this help"
		echo ""
		echo "This program is free software; you can redistribute it and/or"
		echo "modify it under the terms of the GNU General Public License"
		echo "as published by the Free Software Foundation; either version 2"
		echo "of the License, or (at your option) any later version."
		echo ""
		echo "This program is distributed in the hope that it will be useful,"
		echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
		echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
		echo "See the GNU General Public License for more details."
		echo ""
		echo "You should have received a copy of the GNU General Public License"
		echo "along with this program; if not, write to the Free Software Foundation,"
		echo "Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA."
		echo ""

	};;

	esac

exit 0
